package com.jboost.modules.app.service.strategry.pdm;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jboost.common.util.DateUtils;
import com.jboost.common.util.HttpRestUtils;
import com.jboost.common.util.JSONUtil;
import com.jboost.modules.app.constant.BaseResult;
import com.jboost.modules.app.dto.JdyReq;
import com.jboost.modules.app.service.strategry.common.AbstractJdy;
import com.jboost.modules.system.entity.BaseEntity;
import com.jboost.modules.system.entity.PdmPl;
import com.jboost.modules.system.mapper.PdmPlMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationRunner;
import org.springframework.http.HttpHeaders;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

@Service(value = "PdmPlQuery")
@Slf4j
public class PdmPlQuery extends AbstractJdy<String,PdmPl, PdmPlMapper, PdmPl>  {


    public String getFormId() {
        return "651fbb02391f2adff5ba5fd4";
    }

    public HttpHeaders getJdyHeaders(){
        HttpHeaders jdyHeaders = new HttpHeaders();
        jdyHeaders.set("Authorization","Bearer "+ "j2tq9lljcelsqTPZnbr36USxr6yZWsHu");
        return jdyHeaders;
    }

    public String getAppId() {
        return "64e1f2d0d8b6100009e88053";
    }


    @Override
    public BaseResult invokeStrategy(String kh) throws Exception {
        JdyReq jdyReq = new JdyReq();
        jdyReq.setApp_id(getAppId());
        jdyReq.setEntry_id(getFormId());
        jdyReq.setLimit(500);
        //jdyReq.setFields(Arrays.asList("_id","_widget_1715052104645","_widget_1700586012635"));
        JdyReq.FilterBean filterBean = new JdyReq.FilterBean();
        filterBean.setRel("and");
        List<JdyReq.FilterBean.CondBean> cond = new ArrayList<>();
        JdyReq.FilterBean.CondBean a = new JdyReq.FilterBean.CondBean();
        a.setField("style_number");
        a.setType("text");
        a.setMethod("eq");
        a.setValue(Arrays.asList(kh));
        cond.add(a);
        filterBean.setCond(cond);
        jdyReq.setFilter(filterBean);
        String s = jdyQueryAll(jdyReq);
        JSONArray jsonArray = (JSONArray) JSONUtil.fromJson(s, HashMap.class).get("data");
        List<PdmPl> pdmPls = new ArrayList<>();
        for (Object object : jsonArray) {
            JSONObject e = (JSONObject) object;
            PdmPl pdmPl = new PdmPl();
            pdmPl.setKh(e.getString("style_number"));
            pdmPl.setPm(e.getString("_widget_1689822029731"));
            pdmPl.setBom(e.getString("_widget_1689772437724"));
            pdmPl.setSz(e.getString("_widget_1699847126413"));
            pdmPl.setWldl(e.getString("_widget_1695299408480"));
            pdmPl.setWlbm(e.getString("_widget_1695299408481"));
            pdmPl.setWlmc(e.getString("_widget_1695299408482"));
            pdmPl.setBw(e.getString("_widget_1695299408483"));
            pdmPl.setYszwmc(e.getString("_widget_1695299408484"));
            pdmPl.setGg(e.getString("_widget_1695299408488"));
            pdmPl.setKz(e.getString("_widget_1695299408489"));
            pdmPl.setBdbfk(e.getString("_widget_1695299408490"));
            pdmPl.setSyfk(e.getString("_widget_1695299408491"));
            pdmPl.setDw(e.getString("_widget_1695299408492"));
            pdmPl.setWljd(e.getString("_widget_1699847126410"));
            pdmPl.setId(e.getString("_id"));
            pdmPl.setCf(e.getString("_widget_1703603681704"));
            pdmPl.setZt(e.getString("_widget_1695300314310"));
            pdmPl.setGwlbm(e.getString("_widget_1699847126409"));
            pdmPl.setGysjc(e.getString("_widget_1700298112940"));
            if (Objects.equals(pdmPl.getZt(),"正常")){
                pdmPls.add(pdmPl);
            }
        }
        pdmPls.sort(Comparator.comparing(PdmPl::getSz).thenComparing(PdmPl::getWldl, Comparator.reverseOrder()));
        return BaseResult.success(pdmPls);
    }

//    @Scheduled(initialDelay = 1000, fixedRate = 60000)
//    public BaseResult<Boolean> invokeStrategy() throws Exception {
//        PdmPl hxProduct = new PdmPl();
//        sync(hxProduct);
//        return BaseResult.success();
//    }
//
//
//
//    public void sync(PdmPl entit) throws IOException {
//        log.info("开始批量查询Hs材料数据");
//        //baseMapper.delete((Wrapper<PdmPl>) Wrappers.lambdaQuery(entit.getClass()).orderByDesc(BaseEntity::getId).ne(BaseEntity::getId,"0"));
//        HashMap<String, Object> param = getParam();
//        param.put("limit",1000);
//        List<JSONObject> jsonObjects = new ArrayList<>();
//        do {
//            String post = HttpRestUtils.post(query_all_api, param, getJdyHeaders());
//            jsonObjects   = (List<JSONObject>) JSONUtil.fromJson(post, Map.class).get("data");
//            jsonObjects.forEach(e -> {
//                try {
//                    Object o = baseMapper.selectById(e.getString("_id"));
//                    PdmPl pdmPl = new PdmPl();
//                    pdmPl.setKh(e.getString("style_number"));
//                    pdmPl.setPm(e.getString("_widget_1689822029731"));
//                    pdmPl.setBom(e.getString("_widget_1689772437724"));
//                    pdmPl.setSz(e.getString("_widget_1699847126413"));
//                    pdmPl.setWldl(e.getString("_widget_1695299408480"));
//                    pdmPl.setWlbm(e.getString("_widget_1695299408481"));
//                    pdmPl.setWlmc(e.getString("_widget_1695299408482"));
//                    pdmPl.setBw(e.getString("_widget_1695299408483"));
//                    pdmPl.setYszwmc(e.getString("_widget_1695299408484"));
//                    pdmPl.setGg(e.getString("_widget_1695299408488"));
//                    pdmPl.setKz(e.getString("_widget_1695299408489"));
//                    pdmPl.setBdbfk(e.getString("_widget_1695299408490"));
//                    pdmPl.setSyfk(e.getString("_widget_1695299408491"));
//                    pdmPl.setDw(e.getString("_widget_1695299408492"));
//                    pdmPl.setWljd(e.getString("_widget_1699847126410"));
//                    pdmPl.setId(e.getString("_id"));
//                    pdmPl.setCf(e.getString("_widget_1703603681704"));
//                    pdmPl.setZt(e.getString("_widget_1695300314310"));
//                    pdmPl.setGwlbm(e.getString("_widget_1699847126409"));
//                    pdmPl.setGysjc(e.getString("_widget_1700298112940"));
//                    if (Objects.isNull(o)) {
//
//                        baseMapper.insert(pdmPl);
//                    }else if (!Objects.equals(o,pdmPl)){
//                        baseMapper.deleteById(pdmPl.getId());
//                        baseMapper.insert(pdmPl);
//                    }
//                }catch (Exception exception){
//
//                }
//
//            });
//            if (jsonObjects.size()>0){
//
//                param.put("data_id",jsonObjects.get(jsonObjects.size()-1).getString("_id"));
//            }
//        }while (jsonObjects.size()>0);
//
//    }
}
